//
//  ScanSuccessViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/17.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "ScanSuccessViewController.h"
#import "DataMD5.h"

@interface ScanSuccessViewController ()

@end

@implementation ScanSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpNavigationBar];
    self.title = @"确认登录";
    [self _initWithLogInView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}

- (void)setUpNavigationBar {
    
//    self.navigationController.navigationBar.barTintColor = DEFAULT_COLOR;
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside title:nil];
//    self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:@"确认登录"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    self.navigationItem.titleView = customLab;
}

- (void)_initWithLogInView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"computer"]];
    [self.view addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(90);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"您正在登陆网页版可为建材联盟";
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(8);
        make.left.equalTo(self.view.mas_left);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"取消登陆" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.width.mas_equalTo(self.view.width * 0.2);
        make.height.mas_equalTo(35);
    }];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    [login setTitle:@"登陆" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [login addTarget:self action:@selector(didClickLogInButton) forControlEvents:UIControlEventTouchUpInside];
    login.layer.borderWidth = 1;
    login.layer.borderColor = BACKGROUND_COLOR.CGColor;
    login.layer.cornerRadius = 3;
    login.layer.masksToBounds = YES;
    [self.view addSubview:login];
    
    [login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(button.mas_top).offset(-18);
        make.width.mas_equalTo(WIDTH * 0.4);
        make.height.mas_equalTo(35);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
}

- (void)didClickLogInButton {
    NSString *urlString = [DataMD5 getUrlStringWithString:self.urlString];
    
    NSDictionary *dict = [DataMD5 getURLParameters:self.urlString];
    
    NSString *string = [[DataMD5 sharedDataMD5] md5:[NSString stringWithFormat:@"%@%@",USERNAME,dict[@"state"]]];
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"state":dict[@"state"],
                             @"token":string
                                 };
    NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:urlString completeBlock:^(id object) {
        NSLog(@"%@",object);
        NSDictionary *dict = object;
    
        if ([[NSString stringWithFormat:@"%@",dict[@"code"]] isEqualToString:@"1"]) {
            [MBProgressHUD showMessage:@"登录成功" toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
                [self backToPre];
            }];
        } else {
            [MBProgressHUD showMessage:[dict[@"message"] objectForKey:@"view"] toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
                [self backToPre];
            }];
        }
    }];
    
}

- (void)backToPre {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
